package com.mvc.translate;

import com.mvc.domain.Emp;
import com.mvc.domain.User;
import com.mvc.dto.BeanDTO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("user")
public class ValueController {
    ///////////////////////// 前台值传后台 /////////////////////
    /**
     * 演示最原始方式
     */
    @RequestMapping(value = "old")
    public String oldMethod(HttpServletRequest request) {
        System.out.println(request.getParameter("userId"));
        System.out.println(request.getParameter("userName"));
        return "/index.jsp";
    }

    /**
     * 演示SpringMVC方式-1
     */
    @RequestMapping(value = "new1")
    public String oldMethod(@RequestParam(name = "userId") Long userId,
                            @RequestParam(name="userName") String userName) {
        System.out.println(userId+"."+userName);
        return "/index.jsp";
    }

    /**
     * 演示SpringMVC方式-2
     */
    @RequestMapping(value = "new2")
    public String oldMethod(User user, Emp emp) {
        System.out.println(user.getUserId()+","+user.getUserName());
        System.out.println("用户名称："+user.getUserName()+",员工姓名："+emp.getUserName());
        return "/index.jsp";
    }

    /**
     * 演示SpringMVC方式-2
     */
    @RequestMapping(value = "new3")
    public String oldMethod(BeanDTO dto) {
        System.out.println("用户名称："+dto.getUser().getUserName()+",员工姓名："+dto.getEmp().getUserName());
        return "/index.jsp";
    }

    /////////////////////////////////// 后台的值传入前台 ///////////////////////////
    @RequestMapping(value = "front")
    public String translateToView(HttpServletRequest request) {
        request.setAttribute("name","网而无");

        return "/index.jsp";
    }

    @RequestMapping(value = "front1")
    public ModelAndView translateToView() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("name","通过ModelAndView");
        mv.setViewName("/index.jsp");
        return mv;
    }

    @RequestMapping(value = "front2")
    public String translateToView(Model model,@ModelAttribute("u") User user) {
        System.out.println("front2:"+model);
        model.addAttribute("name", "通过model存入值");
        return "/index.jsp";
    }

    @RequestMapping(value = "front3")
    public String translateToView(ModelMap model, Emp emp) {
        System.out.println("front3:"+model);
        model.addAttribute("name", "通过model存入值");
        return "/index.jsp";
    }

    ////////////// 拦截器,文件上传下载(项目中【文件名称，分散存储问题】),异常处理 /////////
}
